/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.net.DatagramPacket;
import java.util.StringTokenizer;

/**
 * Concrete implementation of a vote. Which came via the udp server.
 * 
 * This vote is identified using the ip address of the sender.
 * 
 */
class UdpVote implements VoteInterface {

    private VoterIDInterface voterID;
    private long poolID;
    private int voteValue;

    public UdpVote(DatagramPacket receivePacket) throws Exception {
        this.voterID = new UdpVoterID(receivePacket.getAddress());
       
        String data = new String(receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength());
        StringTokenizer tokens = new StringTokenizer(data);
        System.err.println("\""+data+"\"");
        
        poolID = Long.parseLong(tokens.nextToken());
        voteValue = Integer.parseInt(tokens.nextToken());
        System.err.println("5");
        

    }

    @Override
    public VoterIDInterface getVoterId() {
        return voterID;
    }

    @Override
    public long getPollId() {
        return this.poolID;
    }

    @Override
    public int getVoteChoice() {
        return this.voteValue;
    }

    @Override
    public String toString() {
        return "( from: "+voterID.toString() + ", pool: " + poolID + ", choice: " + voteValue+")";
    }
    
    
}
